package org.van.blog.controller.admin;

import javax.servlet.http.HttpServletRequest;

import com.github.pagehelper.PageInfo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.van.blog.controller.BaseController;
import org.van.blog.dto.cond.CommentCond;
import org.van.blog.model.CommentDomain;
import org.van.blog.service.comment.CommentService;
import org.van.blog.utils.APIResponse;

@Controller
@RequestMapping("/admin/comments")
public class CommentController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(CommentController.class);

    @Autowired
    private CommentService commentService;

    @GetMapping(value = "")
    public String index(
            @RequestParam(name = "page", required = false, defaultValue = "1") int page,
            @RequestParam(name = "limit", required = false, defaultValue = "15") int limit,
            HttpServletRequest request

    ) {
        PageInfo<CommentDomain> comments = commentService.getCommentsByCond(new CommentCond(), page, limit);
        request.setAttribute("comments", comments);
        return "admin/comment_list";
    }

    @PostMapping(value = "/status")
    @ResponseBody
    public APIResponse<?> changeStatus(
            HttpServletRequest request,
            @RequestParam(name = "coid", required = true) Integer coid,
            @RequestParam(name = "status", required = true) String status) {
        try {
            CommentDomain comment = commentService.getCommentById(coid);
            if (null != comment) {
                commentService.updateCommentStatus(coid, status);
            } else {
                return APIResponse.fail("Failed to Pass!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error(e.getMessage());
            return APIResponse.fail(e.getMessage());
        }
        return APIResponse.success();
    }

    @PostMapping(value = "/delete")
    @ResponseBody
    public APIResponse<?> deleteStatus(
            HttpServletRequest request,
            @RequestParam(name = "coid", required = true) Integer coid) {
        try {
            CommentDomain comment = commentService.getCommentById(coid);
            if (null != comment) {
                commentService.deleteComment(coid);
            } else {
                return APIResponse.fail("Failed to Pass!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error(e.getMessage());
            return APIResponse.fail(e.getMessage());
        }
        return APIResponse.success();
    }
}
